<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
    
    var $controller_path 	    = '/login/';
    var $model_path 		    = '/admin/admin_model';
    var $lang_path 			= '/admin/admin';
	
    function __construct()
    {
        parent::__construct();
        log_message('debug', "Index controller Class Initialized.");
    }
	
    public function index()
    {

        //登陆一下，检测入参
        $name = $this->get_input("account");
        $password = $this->get_input("password");

        $admin_info = $this->admin_model->login($name);

        if(!$admin_info) {
            $this->write_logs(304,NULL,"登录失败，用户不存在");
            $this->api("304",NULL,"用户不存在");
        }
        if($admin_info->PassWord != md5(trim($password))) {
            $this->write_logs(304,NULL,"登录失败，密码错误");
            $this->api("304",NULL,"密码错误");
        }

        //去掉密码
        unset($admin_info->PassWord);
        $admin_info->Utime = $this->time($admin_info->Ctime);
        $admin_info->Ctime = $this->time($admin_info->Ctime);
        $admin_info->Image = $this->pic2url($admin_info->Image);

        $this->load->model("admin/admin_role_model");

        $userRoleArr = $this->admin_role_model->get_admin_all($admin_info->Id);
        $userRole = NULL;
        if($userRoleArr)
        {

            foreach($userRoleArr as $val)
            {
                $userRole .= $val->remark.",";
            }
            $userRole = trim($userRole,",");
        }
        $admin_info->userRole = $userRole;

        //token
        $admin_info->Token = $this->token_lib->adds($admin_info->Id);

        //log_message("debug",print_r($admin_info,true));
        $this->write_logs("200",$admin_info,"登录成功");
        $this->api("200",$admin_info,"");
    }

    public function quit($name = NULL)
    {
        $this->write_logs("200",NULL,"退出登录");
        $this->api("200",$name);
    }
}
